<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests\UserInsertRequest1;


class OrderController extends Controller
{ 
   
      //核对订单信息
      public function postAdd(Request $request)
      {
          //dd($request->all());
           $data = $request->all();
           // dd($data);
          
          $goods = [];
          foreach($data['data'] as $k=>$v){
             // dd($v);
            $tmp = DB::table('goods')->where('gid',$v['gid'])->first();

            $tmp->buycnt = $v['buycnt'];
            $goods[] = $tmp;
            }
             //dd($goods);
         // 查询当前用户的所有的地址 session('uid')
            $abc = session('uid');
            //dd($abc);
          $adds = DB::table('address')->where('uid',$abc)->get();
            // dd($adds);
            //解析模板
            return view('/home/order/add',['goods'=>$goods,'adds'=>$adds]);

        
        }


        public function postInsert(Request $request)
        {
              // dd($request->all());
              // if(!empty(session('uid'))){
                  $data = $request->except('_token','gid','buycnt');
                  // $data['uid'] = session('uid');
                  $data['uid'] = 44;

                  $data['oid'] = time().rand(11111,99999);
                  // dd($data);
                  //订单的添加
                  $oid = DB::table('order')->insertGetId($data);
                  
                  //订单详情表的添加
                  $goods = $request->only('gid','buycnt');
                  $data=[];
                  foreach ($goods['gid'] as $k => $v) {
                    $tmp['goodsid'] = $v;
                    $tmp['goodscnt'] = $goods['buycnt'][$k];
                    $tmp['order-id'] = $oid;
                    $data[] = $tmp;
                  }
                // dd($data);

                $res = DB::table('order-info')->insert($data);
                //根据订单id 查询订单号
                $dd = DB::table('order')->where('id',$oid)->value('oid');
                // dd($dd);
                return redirect()->action('Home\OrderController@index',[$dd]);
              // }else{
              //   return redirect('/Home/login')->with('error','您还未登录,请先登录');
              // }



        }

        public  function  index($dd)
        {
          //通过订单号查询收货人 总金额
          $arr = DB::table('order')
              ->join('address','order.address-id','=','address.id')
              ->where('oid',$dd)
              ->get();
              // dd($arr);

            //清空购物车
          $res = DB::table('order-info')
              ->join('order','order-info.order-id','=','order.id')
              ->where('oid',$dd)
              ->get();
                // dd($res[0]->goodsid);
              // dd($res);
              //查询用户购物车表信息
               foreach($res as $k=>$v){

               $aa = DB::table('cart')->where(['uid'=>session('uid'),'gid'=>$v->goodsid])->delete();
              

              //购买商品后 goods表商品数量也相应减去
               $bb = DB::table('goods')->where('gid',$v->goodsid)->first();
               // dd($bb);
               $bl = 'goodscnt';
                $cc = $bb->cnt - $v->$bl;
                $data['cnt'] = $cc;

               $pp = DB::table('goods')->where('gid',$v->goodsid)->update($data);
               
              

               }
              // dd($aa);


          return view('home.order.index',['arr'=>$arr]);
        }


        //订单页
        public function getEdit(Request $request)
        {
          $uid = session('uid');
          $bbb = DB::table('order')->where('uid',$uid)->get();

          // dd($bbb);
          // dd($uid);
          //$bbb order表中的东西
          foreach($bbb as $k => $v) 
          {
            $aoo[$v->id] = DB::table('order')
                ->join('order-info','order.id','=','order-info.order-id')
                ->join('goods','order-info.goodsid','=','goods.gid')
                ->where('oid',$v->oid)
                ->get();
          }
         
          // dd($aoo);
          $ooo=[];

          //$aoo是order_info中的东西
          foreach ($aoo as $k => $v) 
          {
            foreach ($v as $kk => $vv) {
              // $arr[] = $vv->goodsid;
            
            $ooo[] = $vv;
           
            }
            
          }
          //dd($ccc);
          return view('/home/order/edit',['aoo'=>$aoo,'ooo'=>$ooo]);
          }
        


}



  




